package com.orange.lswidget.widgets.event;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.IBinder;
import android.widget.RemoteViews;

import com.orange.homescreen.pil.NotificationSummaryInterface;
import com.orange.homescreen.pil.PILCore;
import com.orange.homescreen.pil.PILInterfaceManager;
import com.orange.homescreen.pil.base.BasePILMonitor;
import com.orange.homescreen.pil.base.PILListener;
import com.orange.lswidget.LSWidgetManager;
import com.orange.lswidget.widgets.event.EventLSWidgetClient.EventCallBackInterface;
import com.orange.notificationsummary.utils.Constants.EventType;
import com.orange.util.Constants;
import com.orange.util.LSEventUtils;
import com.orange.util.LogHelper;

public class EventLSWidgetService extends Service implements
		EventCallBackInterface, EventType {
	private static final String					TAG							= "EventLSWidgetService";
	private Context								mContext;

	private static final String					ACTION_SHOW_DETAIL_SETTING	= "com.lockscreen.sms.show_detail_setting";
	private static final String					ACTION_EVENT_UNLOCK			= "com.lockscreen.event.unlock";

	private static EventLSWidgetClient			sEventLSWidgetClient;
	private static NotificationSummaryInterface	sNotificationInterfc;

	private volatile static BasePILMonitor		sContactMonitor;
	private volatile static BasePILMonitor		sMissedCallMonitor;
	private volatile static BasePILMonitor		sUnreadMsgMonitor;

	private PILListener							mContactListener			= new NotificationPILListener(
																					CONTACT_UPDATE);
	private PILListener							mMissedCallListener			= new NotificationPILListener(
																					MISSCALL_UPDATE);
	private PILListener							mMsgListener				= new NotificationPILListener(
																					UNMSG_UPDATE);
	private PILListener							mVVMListener				= new NotificationPILListener(
																					VVM_UPDATE);

	public static void startService(Context context) {
		Intent i = new Intent(context, EventLSWidgetService.class);
		context.startService(i);
	}

	public static void stopService(Context context) {
		Intent i = new Intent(context, EventLSWidgetService.class);
		context.stopService(i);
	}

	@Override
	public void onCreate() {
		super.onCreate();
		mContext = getApplicationContext();

		if (!PILCore.isInialized()) {
			PILCore.initialize(mContext);
		}

		if (sNotificationInterfc == null) {
			sNotificationInterfc = (NotificationSummaryInterface) PILInterfaceManager
					.getInterface(NotificationSummaryInterface.class);
		}

		sEventLSWidgetClient = new EventLSWidgetClient(mContext);
		sEventLSWidgetClient.setEventCallBackInterface(this);

		sContactMonitor = sNotificationInterfc.getContactsMonitor(this);
		sMissedCallMonitor = sNotificationInterfc.getMissedCallMonitor(this);
		sUnreadMsgMonitor = sNotificationInterfc.getUnreadMsgMonitor(this);

		registerObserver();
	}

	private void registerObserver() {
		if (sContactMonitor != null) {
			sContactMonitor.addListener(mContactListener);
		}
		if (sMissedCallMonitor != null) {
			sMissedCallMonitor.addListener(mMissedCallListener);
		}
		if (sUnreadMsgMonitor != null) {
			sUnreadMsgMonitor.addListener(mMsgListener);
		}

		// mContext.getContentResolver().registerContentObserver(mTimeFormatUri,
		// true,
		// tdObserver);

		IntentFilter appChangedFilter = new IntentFilter();
		appChangedFilter.addAction(Constants.ACTION_VOICEMAIL);
		appChangedFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
		appChangedFilter.addAction(Intent.ACTION_PACKAGE_INSTALL);
		appChangedFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
		appChangedFilter.addDataScheme("package");
		registerReceiver(mReceiver1, appChangedFilter);

		IntentFilter voiceMailUpdateFilter = new IntentFilter();
		voiceMailUpdateFilter.addAction(Constants.ACTION_VOICEMAIL);
		registerReceiver(mReceiver2, voiceMailUpdateFilter);

		IntentFilter filter = new IntentFilter();
		filter.addAction(Intent.ACTION_SCREEN_ON);
		filter.addAction(ACTION_SHOW_DETAIL_SETTING);
		filter.addAction(ACTION_EVENT_UNLOCK);
		registerReceiver(mReceiver3, filter);
	}

	@Override
	public void onStart(Intent intent, int startId) {
		super.onStart(intent, startId);

		// Query all messages database for update RemoteViews
		LogHelper.v(TAG, "onStart. ");
		query(ALL_UPDATE);
	}

	class EventReceiver extends BroadcastReceiver {
		@Override
		public void onReceive(Context context, Intent intent) {
			String action = intent.getAction();

			Uri uri = intent.getData();
			String vvmUriStr = ((uri != null) ? uri.toString() : "");

			if (Intent.ACTION_SCREEN_ON.equals(action)
					|| ACTION_SHOW_DETAIL_SETTING.equals(action)) {
				// observer3.onChange(true);
				LogHelper.d("EventLSWidgetClient", "onReceive. ");
				query(ALL_UPDATE);
			} else if (ACTION_EVENT_UNLOCK.equals(action)) {
				startNewActivity();
			} else if (Constants.ACTION_VOICEMAIL.equals(action)
					|| vvmUriStr.indexOf(Constants.PKG_VOICEMAIL) != -1) {
				mVVMListener.update(null);
			}
		}
	}

	private BroadcastReceiver	mReceiver1	= new EventReceiver();
	private BroadcastReceiver	mReceiver2	= new EventReceiver();
	private BroadcastReceiver	mReceiver3	= new EventReceiver();

	private void query(int type) {
		sEventLSWidgetClient.query(type);
	}

	private void startNewActivity() {
		try {
			sEventLSWidgetClient.startNewActivity();
		} catch (Exception e) {
		}
	}

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	@Override
	public void onDestroy() {
		if (sContactMonitor != null) {
			sContactMonitor.stop();
		}
		if (sMissedCallMonitor != null) {
			sMissedCallMonitor.stop();
		}
		if (sUnreadMsgMonitor != null) {
			sUnreadMsgMonitor.stop();
		}

		unregisterReceiver(mReceiver1);
		unregisterReceiver(mReceiver2);
		unregisterReceiver(mReceiver3);

		super.onDestroy();
	}

	@Override
	public void onQueryComplete(RemoteViews remoteViews, boolean isHaveContent) {
		LSWidgetManager.updateLSWidgetProvider(LSEventUtils.sComponentName,
				remoteViews, isHaveContent);
	}

	private class NotificationPILListener implements PILListener {
		private int	type	= -1;

		public NotificationPILListener(int type) {
			this.type = type;
		}

		public void update(Object data) {
			query(type);
		}
	}
}